<?php
/**
 * Created by PhpStorm.
 * User: yzh
 * Date: 2019/5/17
 * Time: 16:30
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\service\Token;
use app\api\service\Statistics as StatisticsService;

class Statistics extends BaseController
{
    /**
     * 获取排名
     * @param $time
     * @param $page
     * @param $size
     * @return false|string
     * @throws \app\lib\exception\TokenException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRank($time,$page,$size=10){
        $t_id = Token::getCurrentTokenVar('t_id');
        $wu_id = Token::getCurrentUid();
        $service = new StatisticsService();
        $result =  $service->getAllUser($t_id,$wu_id,$time,$page,$size);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 20050;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取我的排名
     * @param $time
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMyRank($time){
        $wu_id = Token::getCurrentUid();
        $service = new StatisticsService();
        $result = $service->getMyRank($wu_id,$time);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 20060;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 点赞
     * @param $id
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function thumbsup($id){
        $wu_id = Token::getCurrentUid();
        $service = new StatisticsService();
        $result = $service->creatThumbsup($wu_id,$id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 20070;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 点赞列表
     * @param $id
     * @param $size
     * @param $page
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($id,$size,$page){
        $wu_id = Token::getCurrentUid();
        $service = new StatisticsService();
        $result = $service->getThumbsupList($id,$size,$page);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 20080;
            $data['content'] = '';
        }
        return json_encode($data);
    }


    /**
     * 每日排序结果
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function insertLog(){
        //$wu_id = Token::getCurrentUid();
        $service = new StatisticsService();
        $time[0] = mktime(0,0,0,date('m'),date('d')-1,date('Y'));//获取前一天起始时间戳
        $time[1] = mktime(0,0,0,date('m'),date('d'),date('Y'))-1;//获取前一天结束时间戳
        $result = $service->insertLog($time);
        return json_encode($result);

    }
}